package service;

import dao.ProductDao;
import entity.Collection;
import entity.Comment;
import entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import relationEntity.ProductsDetails;
import relationEntity.Products;
import util.Page;

import java.util.List;

/**
 * Created by jifeng on 2016/12/12 0012.
 */
public class ProductService {
    @Autowired
    private ProductDao productDao;

    public ProductDao getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public Product addProduct(Product product){
        productDao.addProduct(product);
        return product;
    }
    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page findProductList(int currentPage,String categoryName,String subCategoryName,String colorSizeName,String sell,String productName, int pageSize){
        Page page = new Page();
        //总记录数

        int allRow =productDao.getAllRowCount(categoryName,subCategoryName,colorSizeName,sell,productName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Products> list = productDao.findProductList(offset,categoryName,subCategoryName,colorSizeName,sell,productName, pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public void deleteProduct(String ids){
        productDao.deleteProduct(ids);
    }

    public void recoveryProduct(String ids){
        productDao.recoveryProduct(ids);
    }

    public Page recycleProductList(int currentPage,String categoryName,String subCategoryName,String colorSizeName,String productName, int pageSize){
        Page page = new Page();
        //总记录数
        int allRow =productDao.getRecycleAllRowCount(categoryName,subCategoryName,colorSizeName,productName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Products> list = productDao.recycleProductList(offset,categoryName,subCategoryName,colorSizeName,productName, pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public void sellProduct(String ids){
        productDao.sellProduct(ids);
    }

    public void  noSellProduct(String ids){
        productDao.noSellProduct(ids);
    }

    public List<ProductsDetails> displayProductDetails(int id){
        return productDao.displayProductDetails(id);
    }
    public Product findProductById(int id){
        return productDao.findProductById(id);
    }

    public void updateProduct(Product product){
        productDao.updateProduct(product);
    }


    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */

    public Page frontEndProductList(int currentPage,String productName,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow =productDao.getProductAllRowCount(productName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Product> list = productDao.findFrontEndProductList(offset,productName,pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;

    }


    public Page commetList(int currentPage,int productId,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow =productDao.getCommetAllRowCount(productId);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Comment> list = productDao.findCommentById(offset,productId,pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;

    }

    public void addCollection(Collection collection){
        productDao.addCollection(collection);
    }

    public List<Collection> findByProductIdAndUserId(int productId,int userId){
       return productDao.findByProductIdAndUserId(productId,userId);
    }

    public void deleteCollection(int id){
        productDao.deleteCollection(id);
    }

    public Page commentList(int currentPage,String userName,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow =productDao.getCommentAllRowCount(userName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Comment> list = productDao.findComment(offset,userName,pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;

    }

    public Page recoveryCommentList(int currentPage,String userName,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow =productDao.getCommentRowCount(userName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Comment> list = productDao.findRecoveryComment(offset,userName,pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;

    }

    public void deleteComment(String ids) {
        productDao.deleteComment(ids);
    }

    public void recoveryComment(String ids) {
        productDao.recoveryComment(ids);
    }
}
